package chapter17.Exercise;

/**
 * @Auther: Tx
 * @Date: 2022/6/19 20:07
 */

/**1.要求主线程输出10次hi，当输出到五次的时候启动一个子线程，输出hello 输出10次后退出
 * **/
public class ThreadMethodExercise {
    public static void main(String[] args) throws InterruptedException {
        PrintHello printHello = new PrintHello();
        Thread thread = new Thread(printHello);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(100);
            if (i==5){
                thread.start();//启动子线程
                thread.join();//线程插队
            }
            System.out.println("hi" + i );
        }
    }

}
class PrintHello implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello"+" "+i);
        }
        }

}
